package com.googlecode.jawb.tests;

import static org.junit.Assert.assertEquals;

import java.lang.reflect.Field;
import java.util.ArrayList;

import org.junit.Before;
import org.junit.Test;

import com.googlecode.jawb.BinaryType;
import com.googlecode.jawb.Processor;
import com.googlecode.jawb.clues.SetArray;
import com.googlecode.jawb.clues.SetIgnored;
import com.googlecode.jawb.clues.SetType;
import com.googlecode.jawb.clues.types.DynamicLengthType;
import com.googlecode.jawb.clues.types.StaticLengthType;

@SuppressWarnings("unused")
public class ProcessorTest {
	final ArrayList<String>	fieldsPrimitive			= new ArrayList<>();
	final ArrayList<String>	fieldsObject			= new ArrayList<>();
	final ArrayList<String>	fieldsArrayPrimitive	= new ArrayList<>();
	final ArrayList<String>	fieldsArrayObject		= new ArrayList<>(); 
	
	@SetIgnored()
	private int constant1 = 10;
	private int constant2 = 20;
	
	@SetType(type = BinaryType.UINT_8)
	private int val1;
	
	@SetType(type = BinaryType.UINT_16)
	@SetArray(staticLength = 3)
	private int[] val2;
	
	@SetType(type = BinaryType.UINT_32)
	@SetArray(dynamicLength = "constant1")
	private int[] val3;
	
	@SetType(type = BinaryType.OBJECT)
	private Object val4;
	
	@SetType(type = BinaryType.OBJECT)
	@SetArray(staticLength = 5)
	private Object[] val5;
	
	@SetType(type = BinaryType.OBJECT)
	@SetArray(dynamicLength = "constant2")
	private Object[] val6;

	@Before()
	public void init() {
		new Processor(new ProcessorTest()){
			@Override
			protected void onPrimitiveField(Field field, BinaryType asType) {
				fieldsPrimitive.add(field.getName() + ":" + asType);
			}

			@Override
			protected void onObjectField(Field field, Class<?> asClass) {
				fieldsObject.add(field.getName() + ":" + asClass);
			}

			@Override
			protected void onPrimitiveArray(Field field, int length, BinaryType asType) {
				fieldsArrayPrimitive.add(field.getName() + ":" + length + ":" + asType);
			}

			@Override
			protected void onObjectArray(Field field, int length, Class<?> asClass) {
				fieldsArrayObject.add(field.getName() + ":" + length + ":" + asClass);
			}

			@Override
			protected void onStaticallyConvertedField(Field field, StaticLengthType staticLengthType) {
			}

			@Override
			protected void onDynamicallyConvertedField(Field field, DynamicLengthType dynamicLengthType) {
			}
		}.process();
	}
	
	@Test()
	public void primitiveFields() {
		assertEquals(this.fieldsPrimitive.get(0), "val1:" + BinaryType.UINT_8);
		assertEquals(this.fieldsPrimitive.size(), 1);
	}
	
	@Test()
	public void objectFields() {
		assertEquals(this.fieldsObject.get(0), "val4:" + Object.class);
		assertEquals(this.fieldsObject.size(), 1);
	}
	
	@Test()
	public void arrayPrimitives() {
		assertEquals(this.fieldsArrayPrimitive.get(0), "val2:3:UINT_16");
		assertEquals(this.fieldsArrayPrimitive.get(1), "val3:10:UINT_32");
		assertEquals(this.fieldsArrayPrimitive.size(), 2);
	}
	
	@Test()
	public void arrayObjects() {
		assertEquals(this.fieldsArrayObject.get(0), "val5:5:" + Object.class);
		assertEquals(this.fieldsArrayObject.get(1), "val6:20:" + Object.class);
		assertEquals(this.fieldsArrayObject.size(), 2);
	}
}
